#! /usr/bin/env python

import rospy
import actionlib
from Decetion.msg import *
from CVbright import *

def done_cb(state,result:ObjectResult):
    if state == actionlib.GoalStatus.SUCCEEDED:
        rospy.loginfo("响应结果:%s",result.reslut)
        rospy.loginfo("结果解析：")
        rospy.loginfo("抓取物品是否存在：%s",str(result.pick_object_is_exist))
        rospy.loginfo("像素坐标系位置：%f,%f",result.pick_object_x,result.pick_object_y)
        rospy.loginfo("放置物品是否存在：%s",str(result.place_object_is_exist))
        rospy.loginfo("像素坐标系位置：%f,%f",result.place_object_x,result.place_object_y)

def active_cb():
    rospy.loginfo("服务被激活....")


def fb_cb(fb):
    rospy.loginfo("当前进度:%.2f",fb.progress_bar)

if __name__ == "__main__":
    img =  cv2.imread("/home/ds/work/ws_panda/11-18Ollama的Python调用/default.jpeg")
    imgros = cv2_to_imgmsg(img)
    # 2.初始化 ROS 节点
    rospy.init_node("action_client_p")
    # 3.创建 action Client 对象
    client = actionlib.SimpleActionClient("decetion",ObjectAction)
    # 4.等待服务
    client.wait_for_server()
    # 5.组织目标对象并发送
    goal_obj = ObjectGoal()
    goal_obj.image = imgros

    client.send_goal(goal_obj,done_cb,active_cb,fb_cb)
    # 6.编写回调, 激活、连续反馈、最终响应
    # 7.spin
    rospy.spin()
